<template>
  <el-table :data="list" border fit highlight-current-row style="width: 100%"  @selection-change="selectionChange">
    <el-table-column
      type="selection"
      width="55" v-if="this.type =='1'">
    </el-table-column>
    <el-table-column width="185px" align="center" label="优惠券名称">
      <template slot-scope="scope">
        <span>{{ scope.row.couponName}}</span>
      </template>
    </el-table-column>

    <el-table-column width="185px" align="center" label="券号">
      <template slot-scope="scope">
        <span>{{ scope.row.couponNumber}}</span>
      </template>
    </el-table-column>

    <el-table-column width="185px" align="center" label="订单编号">
      <template slot-scope="scope">
        <span>{{ scope.row.orderId }}</span>
      </template>
    </el-table-column>

    <el-table-column width="100px" align="center" label="使用渠道">
      <template slot-scope="scope">
        <span>{{ scope.row.useChannel}}</span>
      </template>
    </el-table-column>

    <el-table-column align="center" label="生券日期" width="200">
      <template slot-scope="scope">
        <span>{{ scope.row.createCouponTime }}</span>
      </template>
    </el-table-column>

    <el-table-column class-name="status-col" align="center" label="有效时间" width="310">
      <template slot-scope="scope">
        <span>{{ scope.row.useStartTime }}~{{ scope.row.useEndTime }}</span>
      </template>
    </el-table-column>
    <el-table-column class-name="status-col" align="center" label="绑定手机号" width="110">
      <template slot-scope="scope">
        <span>{{ scope.row.useMobile }}</span>
      </template>
    </el-table-column>
    <el-table-column class-name="status-col" align="center" label="状态" width="170">
      <template slot-scope="scope">
        <span v-if="scope.row.status === '0'">已发行</span>
        <span v-else-if="scope.row.status === '1'">可使用</span>
        <span v-else-if="scope.row.status === '2'">已使用</span>
        <span v-else-if="scope.row.status === '3'">已过期</span>
        <span v-else-if="scope.row.status === '4'">已作废</span>
      </template>
    </el-table-column>
    <el-table-column class-name="status-col" align="center" label="操作" width="150">
      <template slot-scope="scope" >
        <span v-if="scope.row.status =='1'" >
          <el-button type="text" class="textButton" @click="invalid(scope.row.couponId)" >作废</el-button>
        </span>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
// import { fetchList } from '@/api/article'
import couponUtils from "@/assets/js/couponUtils.js";

export default {
  name : 'TableCoupon',
  components: {
    couponUtils
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  props: {
    type: {
      type: String,
      default: '5'
    }
  },
  data() {
    return {
      list: [],
      tableList: {
          status0: [{
            "couponname": "免单卷",
            "couponnumber": '652521154552',
            "usetype": "线上",
            "ordernumber": '52123525114',
            "status": "进行中",

            "phone": "13698542245",
            "createUserid": 19090215374897,
            "createTime": '2019-12-25 10:54:49',

            "companyId": 276,
            "createMerchantId": null,
            "createMerchantName": null,

            "merchantType": 1,

            "payType": null,
            "canCreateActivity": 1,
            "ruleDesc": null,
            "scopeType": null,
            "channelCodes": "BBC",
            "hasWriteRoll": true,
            "type": "1"
          }, {
            "activityId": 1912240002354936,
            "couponname": "折扣卷",
            "couponnumber": 652521154552,
            "usetype": "折扣",
            "ordernumber": '52123525114',
            "status": "进行中",

            "phone": "13698542245",
            "createUserid": 19090215374897,
            "createTime": '2019-12-25 10:54:49',

            "companyId": 276,
            "createMerchantId": null,
            "createMerchantName": null,

            "merchantType": 1,
            "promPlatform": 11,
            "payType": null,
            "canCreateActivity": 1,
            "ruleDesc": "答复",
            "scopeType": null,
            "channelCodes": "BBC",
            "hasWriteRoll": true,
            "type": "1"
          }],
          status1: [{
            "couponname": "大额劵",
            "couponnumber": '652521154552',
            "usetype": "线上",
            "ordernumber": '52123525114',
            "status": "已发行",
            "phone": "13698542245",
            "createUserid": 19090215374897,
            "createTime": '2019-12-25 10:54:49',
            "companyId": 276,
            "createMerchantId": null,
            "createMerchantName": null,
            "merchantType": 1,
            "payType": null,
            "canCreateActivity": 1,
            "ruleDesc": null,
            "scopeType": null,
            "channelCodes": "BBC",
            "hasWriteRoll": true,
            "type": "1"
          }, {
            "activityId": 1912240002354936,
            "couponname": "抵用券",
            "couponnumber": 652521154552,
            "usetype": "折扣",
            "ordernumber": '52123525114',
            "status": "已发行",
            "phone": "13698542245",
            "createUserid": 19090215374897,
            "createTime": '2019-12-25 10:54:49',
            "companyId": 276,
            "createMerchantId": null,
            "createMerchantName": null,
            "merchantType": 1,
            "promPlatform": 11,
            "payType": null,
            "canCreateActivity": 1,
            "ruleDesc": "答复",
            "scopeType": null,
            "channelCodes": "BBC",
            "hasWriteRoll": true,
            "type": "1"
          }],
          status2: [{
            "couponname": "免单卷",
            "couponnumber": '652521154552',
            "usetype": "线上",
            "ordernumber": "52123525114",
            "status": "	可使用",
            "phone": "merchant",
            "createUserid": 1909160000172983,
            "createTime": "2019-12-10 10:52:13",
            "companyId": 276,
            "createMerchantId": null,
            "createMerchantName": null,
            "merchantType": 1,
            "promPlatform": 11,
            "payType": null,
            "canCreateActivity": 1,
            "ruleDesc": "asdasdas",
            "scopeType": null,
            "channelCodes": "BBC",
            "hasWriteRoll": true,
            "type": "2"
          }, {
            "activityId": 1912100005294293,
            "couponname": "免单卷",
            "couponnumber": 652521154552,
            "usetype": "线上",
            "ordernumber": "52123525114",
            "status": "	可使用",
            "phone": "13698542245",
            "createUserid": 19090215374897,
            "createTime": "2019-12-24 14:50:14",
            "companyId": 276,
            "createMerchantId": null,
            "createMerchantName": null,
            "merchantType": 1,
            "payType": null,
            "canCreateActivity": 1,
            "ruleDesc": null,
            "scopeType": null,
            "channelCodes": "BBC",
            "hasWriteRoll": true,
            "type": "2"
          }],
          status5: [{
            "couponname": "大额劵",
            "couponnumber": '35212153232',
            "usetype": "线上",
            "ordernumber": "52123525114",
            "status": "已作废",
            "phone": "13698542245",
            "createUserid": 19090215374897,
            "createTime": "2019-12-05 14:22:22",
            "companyId": 276,
            "createMerchantId": null,
            "createMerchantName": null,
            "merchantType": 1,
            "payType": null,
            "canCreateActivity": 1,
            "ruleDesc": null,
            "scopeType": null,
            "channelCodes": "BBC",
            "hasWriteRoll": true,
            "type": "5"
          }, {
            "activityId": 1912050006571923,
            "couponname": "大额劵",
            "couponnumber": '35212153232',
            "usetype": "线上",
            "ordernumber": "52123525114",
            "status": "已作废",
            "phone": "13698542245",
            "createUserid": 19090215374897,
            "createTime": "2019-12-05 13:21:38",
            "companyId": 276,
            "createMerchantId": null,
            "createMerchantName": null,
            "merchantType": 1,
            "payType": null,
            "canCreateActivity": 1,
            "ruleDesc": "321",
            "scopeType": null,
            "channelCodes": "BBC",
            "hasWriteRoll": true,
            "type": "5"
          }],
        },

     isSelect:false,
      listQuery: {
        page: 1,
        limit: 5,
        type: this.type,
        sort: '+id'
      },
      loading: false,
      filters:{
        url:'jic-market/coupon/query-detail-no-page',
      },
      couponIdList: []
    }
  },
  created() {
    const tab = this.$route.query.tab
    console.log("====tab"+tab);
    this.listQuery.type = tab;
    this.findPage(this.filters)
    // this.getList()
  },
  methods: {
    // 分页查询filters
    findPage: function (filters) {
      let this_ = this;
      if (filters == undefined || filters == null) {
        filters = {};
      }
      filters.url = 'jic-market/coupon/query-detail-no-page';
      filters.start = this.listQuery.page;
      filters.limit = this.listQuery.limit;
      this.utils.request.httpUtils(filters, function (res) {
        console.log("==========res:" + JSON.stringify(res.data));
        if (res.data == null) {
          res.data = {};
        }
        let key = 'status'+this_.listQuery.type;
        console.log("====key"+key);
        this_.list = res.data[key];
        this_.totalSize = Number(res.data);
      });
    },
    // getList() {
    //   this.loading = true
    //   this.$emit('create') // for test
    //   // fetchList(this.listQuery).then(response => {
    //   this.list = this.tableList[key];
    //   this.loading = false
    //   // })
    // },
    getList() {
      this_.loading = true
      this_.$emit('create') // for test
      // fetchList(this.listQuery).then(response => {
        let key = 'status'+this_.listQuery.type;
      this.list = this_.tableList[key];
      this_.loading = false
      // })
    },
    invalidBatch: function(){
        let this_=this;
        console.log("!!!!!!!test",this_.couponIdList);
        this_.couponIdList.forEach(item => {
          let filters = {};
          filters.url = 'jic-market/coupon/delete-coupon-detail';
          filters.couponId = item;
          this.utils.request.httpUtils(filters, function (res) {
              console.log("==========res:" + res);
          });
        });
        let filtersfind = {};
        filtersfind.url = 'jic-market/coupon/query-detail-no-page';
        this_.findPage(filtersfind);
        this.$router.go(0);
    },
    //作废
    invalid: function(couponId) {
        const tab = this.$route.query.tab
        this.type = tab;
        this.listQuery.type = tab;
        console.log("==========couponId:" + couponId);
        this.$confirm('确定作废此条数据?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
          console.log("==========couponId:" + couponId);
          let filters = {};
          filters.url = 'jic-market/coupon/delete-coupon-detail';
          filters.couponId = couponId;
          this.utils.request.httpUtils(filters, function (res) {
            console.log("==========res:" + res);
          });

          let filtersfind = {};
          filtersfind.url = 'jic-market/coupon/query-detail-no-page';
          this.findPage(filtersfind);
          this.$message({message:'操作成功',type:'success'});

          this.$router.go(0);
          }).catch(() => {

          });
    },
    //追加商品
    appendMerchantProduct: function() {

    },

    //查看营销报告
    viewMarketingReport: function() {

    },
    selectionChange(data){
      console.log("!!!!!!!test",data);
      let list = [];
      data.forEach(item => {
        console.log("!!!!!!!item.couponId",item.couponId);
        list.push(item.couponId);
      });
      this.couponIdList = list;
    },
  },
}
</script>
<style scoped>
  .textButton {
    margin-left: 1px;
  }
</style>

